<% layout('/layouts/default.html', {title: '分配数据权限', libs: ['validate','zTree']}){ %>
<div class="card bd-0">
    <div class="card-header d-flex align-items-start justify-content-between box-header fixed">
        <div class="pt-1">分配数据权限</div>
        <div class="d-flex">

        </div>
    </div>
    <#form:form id="inputForm" model="${role}" action="${ctx}/sys/role/saveAuthDataScope" method="post">
        <div class="card-body card-content-fixed pd-sm-t-30 pd-sm-b-40 pd-sm-x-30">
        	<div class="form-row">
	            <div class="form-group  col-md-6">
	                <label>角色名称 <span class="tx-danger ">*</span></label>
	                <#form:input path="roleName" maxlength="10" readonly="true" class="form-control"/>
	            </div>
	            <div class="form-group  col-md-6">
	                <label>角色编码 <span class="tx-danger ">*</span></label>
                	<#form:input path="roleCode" maxlength="32" readonly="true" class="form-control "/>
	            </div>
	            <div class="form-group  col-md-6">
	                <label>数据范围 </label>
                	<#form:select path="dataScope" dictType="sys_role_data_scope" class="form-control select2"/>
	            </div>
            </div>
            <div id="dataScopeCustom" style="display:<% if(role.dataScope=='2'){ %>block<%}else{%>none<%}%>">
	            <div class="form-unit">授权机构数据</div>
				<div id="dataScopeTrees" class="ztree"></div>
				<#form:hidden name="roleDataScopeListJson"/>
			</div>
        </div>
        <div class="card-footer pd-sm-l-30">
            <div class="row">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-sm btn-primary">保存提交</button>
                    <button type="button" class="btn btn-sm btn-default" onclick="js.closeCurrentTabPage()">关闭窗口</button>
                </div>
            </div>
        </div>
    </#form:form>
</div>
<% } %>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		// 获取数据权限数据
		var dataScopeData = [];
		var treeNodes = dataScopeTrees.getCheckedNodes(true);
		for(var i=0; i<treeNodes.length; i++) {
			dataScopeData.push({
				ctrlType: 'Office', ctrlData: treeNodes[i].id
			});
		}
		$("#roleDataScopeListJson").val(JSON.stringify(dataScopeData));
		// 提交表单数据
		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
		}, "json");
    }
});
$("#dataScope").on('change',function(e){
	if('2'===$(this).val()){
		$("#dataScopeCustom").show();
	}else{
		$("#dataScopeCustom").hide();
	}
		
})
//加载数据权限树结构
var setting = {
	check:{enable:true,nocheckInherit:true,chkboxType:{"Y":"ps","N":"ps"}},
	view:{selectedMulti:false,nameIsHTML: true},
	data:{simpleData:{enable:true},key:{title:"title"}},
	callback:{
		beforeClick: function (treeId, treeNode, clickFlag) {
			var tree = $.fn.zTree.getZTreeObj(treeId);
			tree.checkNode(treeNode, !treeNode.checked, true, true);
			return false;
		},
		onCheck: function (event, treeId, treeNode){
			var tid = treeNode.tId;
			if(!treeNode.checked){
				$(".checkall[value="+treeId+"]").each(function(){
					if(this.checked){
					    $(this).removeAttr("checked").iCheck('update');
					}
					return false;
				}); 
			}
		}
	}
},
dataScopeTrees = {}; // 用sysCode分类存储所有菜单树
$.ajax({
	type: 'POST',
	url: "${ctx}/sys/office/treeData?___t=" + new Date().getTime(),
	data: {ctrlPermi: '2'},
	dataType: 'json',
	async: false,
	error: function(data){
		js.showErrorMessage(data.responseText);
	},
	success: function(data, status, xhr){
		// 初始化树结构
		var tree = $.fn.zTree.init($("#dataScopeTrees"), setting, data);
		// 展开第一级节点
		var nodes = tree.getNodesByParam("level", 0);
		for(var i=0; i<nodes.length; i++) {
			tree.expandNode(nodes[i], true, false, false);
		}
		// 将树对象存储到全局数组里
		dataScopeTrees = tree;
		// 默认选择节点
		//var roleMenuCodes = data.roleMenuCodes;
		//for(var i=0; i<roleMenuCodes.length; i++) {
			//var node = tree.getNodeByParam("id", roleMenuCodes[i]);
			//try{tree.checkNode(node, true, false);}catch(e){}
		//}
	}
});
<% for(dataScope in roleDataScopeList){ %> 
var node = dataScopeTrees.getNodeByParam("id", "${dataScope.ctrlData}");
try{dataScopeTrees.checkNode(node, true, false);}catch(e){}
<% } %>
</script>
</body></html>